package org.example.cake.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.example.cake.common.Rep;
import org.example.cake.common.Repsecond;
import org.example.cake.entity.User;
import org.example.cake.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 文晓平
 * @version 1.0
 * @Description TODO
 * @date 2025/2/21 9:39
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 后端分页查询所有用户
     *
     * @param page  当前页
     * @param limit 每页显示条数
     * @return 用户列表
     */
    @GetMapping("/SelectUserHou")
    public Rep SelectUserHou(@RequestParam(required = false) String username
                             ,@RequestParam(required = false)String userphone,
                              @RequestParam(required = false) Integer status, Integer page , Integer limit) {
        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;
        PageHelper.startPage(page, limit);
        List<User> userList = userService.SelectUserHou(username, userphone,status);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return new Rep().addData("user", pageInfo);

    }
    @PostMapping("/deleteUserHou")
    public Rep deleteUserHou(@RequestBody Map<String, Object> map) {
        Integer id = (Integer) map.get("id");
        userService.deleteByPrimaryKey(id);
        return new Rep().addData("msg", "删除成功");
    }

    @PostMapping("/updateUserHou")
    public Rep updateUserHou(@RequestBody Map<String, Object> map) {
        // 1. 校验参数
        if (!map.containsKey("ids") || !(map.get("ids") instanceof List))
            return new Rep().addData("msg", "参数错误");

        // 2. 取出用户 ID 列表
        List<Long> ids = (List<Long>) map.get("ids");

        // 3. 调用业务层批量切换状态
        return new Rep().addData("update", userService.toggleStatus(ids));
    }

    /**
     * 查询所有用户数据
     * @author 罗江锦
     * @return 所有用户数据
     */
    @GetMapping("/selectAllUser")
    public Rep selectAllUser() {
        List<User> listUser = this.userService.getAllUsers();
        return new Rep().addData("listUser", listUser);
    }

    /**
     * 查询所有用户数据
     * @author 罗江锦
     * @return 统一响应格式
     */
    @GetMapping("/getUserList")
    public Rep getUserList() {
        try {
            List<User> userList = userService.getAllUsers();
            return new Rep()
                    .addData("userList", userList)
                    .addData("count", userList.size());
        } catch (Exception e) {
            return new Rep()
                    .addData("error", "查询用户列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取用户详情
     * @author 罗江锦
     * @param userId 用户ID
     * @return 统一响应格式
     */
    @GetMapping("/{userId}")
    public Rep getUserById(@PathVariable Integer userId) {
        try {
            User user = userService.getUserById(userId);
            if (user == null) {
                return new Rep()
                        .addData("error", "用户不存在");
            }
            return new Rep()
                    .addData("user", user);
        } catch (Exception e) {
            return new Rep()
                    .addData("error", "获取用户详情失败: " + e.getMessage());
        }
    }
    /**
     * 修改用户信息
     * @param user 用户对象
     * @return 受影响的行数
     */
    @PostMapping("/updateUser")
    public int updateUser(@RequestBody User user){
        int updateUser = userService.updateUser(user);
        if (updateUser > 0){
            return updateUser;
        }else {
            return 0;
        }
    }

    /**
     * 根据用户id查询用户信息
     * @param userid 用户id
     * @return 用户对象
     */
    @GetMapping("/selectUserByUserId")
    public Rep selectUserByUserId(@Param("userid") Integer userid){
        User selectUserByUserId = userService.selectUserByUserId(userid);
        return new Rep().addData("selectUserByUserId", selectUserByUserId);
    }
}
